package com.zwp.base.ext

import android.content.Context
import android.content.res.Resources
import android.util.TypedValue
import androidx.core.content.ContextCompat
import androidx.datastore.preferences.preferencesDataStore
import androidx.paging.Pager
import androidx.paging.PagingConfig
//import com.zwp.base.base.BasePagingSource
import com.zwp.base.constant.Constants
import com.zwp.base.paging.BasePagingSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow

val Context.dataStore by preferencesDataStore(name = Constants.MY_PREFERENCES)

// 主要用来防重复点击
fun <T> Flow<T>.throttleFirst(timeMillis: Long) : Flow<T> =
    flow {
        var lastTime = 0L

        collect {
            val currentTime = System.currentTimeMillis()
            if (currentTime - lastTime > timeMillis) {
                lastTime = currentTime
                emit(it)
            }
        }

    }

fun Float.dp() =
    ((TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics)) + 0.5).toInt()

fun Float.sp() =
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics).toInt()
